package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.mapper.StatementMapper;
import cn.wolfcode.car.business.query.StatementQuery;
import cn.wolfcode.car.business.service.IStatementService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class StatementServiceImpl extends ServiceImpl<StatementMapper,Statement> implements IStatementService {

    @Resource
    private StatementMapper statementMapper;

    /*
    select id, customer_name, customer_phone, actual_arrival_time, license_plate, car_series,
    service_type, appointment_id, status, pay_time, payee_id, total_amount, total_quantity,
    discount_amount, create_time, info
    from bus_statement
    <where>
      <if test="customerName != null and ''!=customerName">
        and customer_name like concat('%',#{customerName},'%')
      </if>
      <if test="customerPhone != null and ''!=customerPhone">
        and customer_phone like concat('%',#{customerPhone},'%')
      </if>
      <if test="licensePlate != null and ''!=licensePlate">
        and license_plate like concat('%',#{licensePlate},'%')
      </if>
     */
    @Override
    public TablePageInfo<Statement> query(StatementQuery qo) {
        IPage<Statement> page = new Page<>(qo.getPageNum(), qo.getPageSize());
        LambdaQueryWrapper<Statement> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(!qo.getCustomerName().trim().isEmpty(),Statement::getCustomerName,qo.getCustomerName())
               .like(!qo.getCustomerPhone().trim().isEmpty(),Statement::getCustomerPhone,qo.getCustomerPhone())
               .like(!qo.getLicensePlate().trim().isEmpty(),Statement::getLicensePlate,qo.getLicensePlate());
        return new TablePageInfo<>(super.page(page,wrapper));
    }


    @Override
    public boolean save(Statement statement) {
        statementMapper.insert(statement);
        return true;
    }

    @Override
    public Statement get(Long id) {
        //select id, customer_name, customer_phone, actual_arrival_time, license_plate, car_series,
        //    service_type, appointment_id, status, pay_time, payee_id, total_amount, total_quantity,
        //    discount_amount, create_time, info
        //    from bus_statement
        //    where id = #{id}
        return statementMapper.selectById(id);
    }


    @Override
    public void update(Statement statement) {
        Statement item = this.get(statement.getId());
        //如果当前状态是已支付，则不能编辑
        if (item.getStatus().equals(Statement.STATUS_PAID)) {
            throw new BusinessException("当前状态是已支付，不能进行编辑操作");
        }
        statementMapper.updateById(statement);
    }

    @Override
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            statementMapper.deleteById(dictId);
        }
    }

    @Override
    public List<Statement> list() {
        return statementMapper.selectList(null);
    }

    @Override
    public void updateAmount(Long id, BigDecimal totalAmount, BigDecimal totalQuantity, BigDecimal discountAmount) {
        // update bus_statement set total_amount = #{totalAmount},total_quantity = #{totalQuantity},discount_amount = #{discountAmount} where id = #{id}
        LambdaUpdateWrapper<Statement> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(Statement::getId,id).set(Statement::getTotalAmount,totalAmount)
               .set(Statement::getTotalQuantity,totalQuantity).set(Statement::getDiscountAmount,discountAmount);
        statementMapper.update(null,wrapper);
    }

    @Override
    public void payStatement(Long statementId, Long userId, Integer status) {
        // update bus_statement set status = #{status},pay_time = now(),payee_id = #{userId} where id = #{statementId}
        LambdaUpdateWrapper<Statement> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(Statement::getId,statementId).set(Statement::getStatus,status).set(Statement::getPayTime,new Date());
        statementMapper.update(null,wrapper);
    }

    @Override
    public Statement getByAppointment(Long appointmentId) {
        return statementMapper.selectById(appointmentId);
    }
}
